﻿using System;
using System.Configuration.Install;
using System.IO;
using System.ServiceProcess;
using System.Xml;
using log4net;

namespace HpSocketIotGateway
{
    public class InstallerHelper : IDisposable
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(InstallerHelper));

        /// <summary> 
        /// 初始化服务配置帮助类 
        /// </summary> 
        public InstallerHelper()
        {
            InitSettings();
        }
        /// <summary> 
        /// 系统用于标志此服务的名称 
        /// </summary> 
        public string ServiceName { get; set; }

        //public string[] ServicesDependedOn { get; set; }
        /// <summary> 
        /// 初始化服务配置信息 
        /// </summary> 
        private void InitSettings()
        {
            string root = System.Reflection.Assembly.GetExecutingAssembly().Location;
            if (string.IsNullOrEmpty(root))
            {
                Log.Error(new FileNotFoundException("没有程序集!"));
                throw new FileNotFoundException("没有程序集!");
            }
            string xmlfile = root.Remove(root.LastIndexOf('\\') + 1) + "ServiceSetting.xml";
            if (File.Exists(xmlfile))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlfile);
                XmlNode xn = doc.SelectSingleNode("Settings/ServiceName");
                if (xn != null)
                {
                    ServiceName = xn.InnerText;
                }
                else
                {
                    Log.Error(new Exception("ServiceSetting.xml中没有配置Settings/ServiceName！"));
                    throw new Exception("ServiceSetting.xml中没有配置Settings/ServiceName！");
                }
                //xn = doc.SelectSingleNode("Settings/ServicesDependedOn");
                //if (xn != null)
                //{
                //    ServicesDependedOn = xn.InnerText.Split(',');
                //}
                //else
                //{
                //    throw new Exception("ServiceSetting.xml中没有配置Settings/ServicesDependedOn！");
                //}
            }
            else
            {
                Log.Error(new FileNotFoundException("未能找到服务名称配置文件 ServiceSetting.xml！"));
                throw new FileNotFoundException("未能找到服务名称配置文件 ServiceSetting.xml！");
            }
        }
        public void Dispose()
        {
            ServiceName = null;
        }

        public static void InstallServive(Installer installer)
        {
            ServiceProcessInstaller spi = new ServiceProcessInstaller();
            spi.Account = ServiceAccount.LocalService;//设置服务要运行在什么类型的账号下
                                                      //这里可以创建多个ServiceInstaller实例
            ServiceInstaller si = new ServiceInstaller();
            si.StartType = ServiceStartMode.Automatic;//服务的启动方式，这里设置为自动            
            using (InstallerHelper setting = new InstallerHelper())
            {
                si.ServiceName = setting.ServiceName;//系统操作服务的标识，要和ServiceBase中设置的ServiceName属性值相同
                si.DisplayName = setting.ServiceName;// 展示给用户的服务名，即在控制面板中看到的服务名
                si.Description = "By Jooner;QQ:272946056;Mobile:13912290907";
                //si.ServicesDependedOn = setting.ServicesDependedOn ;
            }
            //最后记得把创建的实例添加到安装列表中
            installer.Installers.Add(si);
            installer.Installers.Add(spi);
        }
    }
}
